import { createApp } from 'vue';

const Module = {
    Core: {},
    Logic: {}
};

if (!sessionStorage.getItem('baseUser')) {
    throw new Error('请登录');
}

export const initializeFederation = () => {
    return Promise.all([
        import('wxy_core'),
        import('wxy_core/components/web'),
        import('wxy_logic')
    ])
        .then(([Core, CoreComponent, Logic]) => {
            Module.Core = {
                ...Core,
                ...CoreComponent
            };

            Core.init({
                env: process.env.app,
                title: process.env.VUE_APP_TITLE,
                pages: process.env.pages,
                tabNavs: process.env.tabNavs,
                baseUser: JSON.parse(sessionStorage.getItem('baseUser')),
                currentOrgId: sessionStorage.getItem('org_id')
            });

            Logic.init(Core);

            Object.freeze(Module);

            return Module;
        });
};

export const loadCore = () => Module.Core;
export const loadLogic = () => Module.Logic;

export const createFederationVueApp = (App) => {
    return initializeFederation().then(({ Core }) => {
        const app = createApp(App);
        app.mount(Module.Core.getContainer());

        return app;
    });
};
